<?php


namespace app\admin\user\controller;


use app\admin\system\logic\UserLogic;
use app\admin\user\logic\UserBalanceLogLogic;
use app\admin\user\logic\UserIntegralLogLogic;
use app\admin\user\validate\UserValidate;
use app\common\controller\BaseAdminController;
use core\utils\Time;
use plugin\exportcenter\app\service\ExportService;

/**
 * 用户管理
 * Class UserController
 * @package app\admin\user\controller
 */
class UserController extends BaseAdminController
{
    protected function init()
    {
        parent::init();
        $this->logic = new UserLogic();
        $this->validate = new UserValidate();
    }

    protected function searchWhere()
    {
        $where = [];
        $this->form['username'] && $where[] = ['username', 'like', "%" . $this->form['username'] . "%"];
        $this->form['nickname'] && $where[] = ['nickname', 'like', "%" . $this->form['nickname'] . "%"];
        $this->form['mobile'] && $where[] = ['mobile', 'like', "%" . $this->form['mobile'] . "%"];
        $this->form['sex'] && $where[] = ['sex', 'in', $this->form['sex']];
        if (!is_null($this->form['filterAvatar']) && $this->form['filterAvatar'] != "") {
            if ($this->form['filterAvatar'] == 0) {
                $where[] = ['avatar', '=', ''];
            } else {
                $where[] = ['avatar', '<>', ''];
            }
        }
        if ($this->form['create_start'] && $this->form['create_end']) {
            $where[] = ['create_time', 'between', [$this->form['create_start'], $this->form['create_end']]];
        }
        return $where;
    }

    public function list()
    {
        $query = $this->logic
            ->where($this->searchWhere())
            ->with([
                'level',
            ]);
        return $this->success($this->logic->getPageList($query));
    }

    public function getById()
    {
        $id = input('id');
        UserValidate::goCheck(input(), 'id');
        $data = $this->logic->getModel()->with([
            'level'
        ])->where('id', '=', $id)->find();
        return $this->success($data);
    }


    /**
     * 积分记录
     */
    public function integralRecord()
    {
        $id = input('id');
        UserValidate::goCheck(input(), 'id');
        $logic = new UserIntegralLogLogic();
        $query = $logic->getModel();
        $query->order("created_at desc");
        $query->where([
            'user_id' => $id
        ]);
        $data = $logic->getPageList($query);
        return $this->success($data);
    }

    /**
     * 余额记录
     */
    public function balanceRecord()
    {
        $id = input('id');
        UserValidate::goCheck(input(), 'id');
        $logic = new UserBalanceLogLogic();
        $query = $logic->getModel();
        $query->order("created_at desc");
        $query->where([
            'user_id' => $id
        ]);
        $data = $logic->getPageList($query);
        return $this->success($data);
    }

    public function exportData()
    {
        $where = $this->searchWhere();
        ExportService::export("用户资料导出" . Time::now(), "user", [
            'table' => "t_user",
            'where' => $where,
        ]);
        return $this->success();
    }
}